﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class frm_Edit_OrderInvoice : Form
    {
        private INISettings mySettings;
        private SqlConnection myConnection;

        public string id;

        public frm_Edit_OrderInvoice()
        {
            InitializeComponent();
            this.mySettings = new INISettings();
            this.myConnection = new SqlConnection(mySettings.SQLConnectionString);
            this.id = null;
        }

        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(this.id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [241_OrderInvoiced] WHERE ID=" + this.id;
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.orderNo.Text = dataReader["OrderNo"].ToString().Trim();
                        this.deliveryDate.Text = dataReader["DeliveryDate"].ToString().Trim();
                    }
                    dataReader.Close();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                }
            }

            return ret;
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {            
            bool duplicate = false;
            if (orderNo.Text == "")
            {
                MessageBox.Show("Please enter Order No.");
            }
            else
            {
                //Check Duplicate
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [241_OrderInvoiced]"
                        + " WHERE OrderNo='" + orderNo.Text.Trim() + "'"
                        + " AND DeliveryDate='" + deliveryDate.Text.Trim() + "' AND ID <> " + this.id;
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        duplicate = true;
                    }
                    dataReader.Close();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    duplicate = true;
                }

                if (duplicate)
                {
                    MessageBox.Show("The Order No. " + orderNo.Text + " existed");
                }
                else
                {
                    try
                    {                       

                        myConnection.Open();
                        string sql = "UPDATE [241_OrderInvoiced] SET OrderNo='" + orderNo.Text.Trim() + "',"
                            + " DeliveryDate='" + deliveryDate.Text.Trim() + "'"
                            + " WHERE ID=" + this.id;                                
                        SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                        cmd_sql.ExecuteNonQuery();
                        cmd_sql.Dispose();
                        myConnection.Close();
                        MessageBox.Show("Update Success!");
                        this.Dispose();
                        //clearForm();
                    }
                    catch //(Exception ex)
                    {
                        if (myConnection.State == ConnectionState.Open)
                        {
                            myConnection.Close();
                        }
                        //MessageBox.Show(ex.Message);
                        MessageBox.Show("Update failed!");
                    }
                }
            }
        }
    }
}
